var app = getApp();
var http_service = app.data.service;
Page({
  data: {
    http_service: http_service,
    /*store_id:0,
    store_name:'',*/
    mry_service:[],
    img:[true,true],
    unlogin:false,
    getNewPersonQuan:false
  },
  onLoad: function (options) {
    var that = this;

    //检测是否是新用户
    wx.getStorage({
      key: 'userid',
      success: function(res) {
        wx.request({
          url: http_service + '/index.php/hasquan',
          data: {
              userId:res.data,
              quanId:1
          },
          success:function(res){
              if(res.data.rescode==0){
                that.setData({
                  getNewPersonQuan:true
                })
              }else{

              }
          }
        })
      },
    })
    

    //获取是否已经授权
    wx.getSetting({
      success(res){
        //没有授权
        if (!res.authSetting['scope.userInfo']){
          that.setData({
            unlogin: true,
          })
        }else{
          that.setData({
            unlogin: false
          })
        }
      }
    })

    //请求服务数据
    wx.request({
      url: http_service +'/index.php/getservice',
      success:function(res){
        that.setData({
          mry_service:res.data
        },function(){
          
        })
      },
      //
    })
  },
  onShow: function () {
    app.data.hide = false;
    var that = this;
    //获取是否已经授权
    wx.getSetting({
      success(res) {
        //没有授权
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            unlogin: true
          })
        } else {
          that.setData({
            unlogin: false
          })
        }
      }
    })
  },

  click:function(e){
    var serviceId = e.currentTarget.id;
    //console.log(serviceId);
    if (serviceId==1){
      wx.navigateTo({
        url: 'childservice?service_id=' + serviceId,
      })
    }else{
      wx.navigateTo({
        url: 'childservice1?service_id=' + serviceId,
      })
    }
  },


  onGotUserInfo: function (e) {
    var that = this;
    console.log("err:"+e.detail.errMsg)
    console.log(e.detail.userInfo)
    if (e.detail.errMsg == "getUserInfo:fail auth deny")
      return;
    var userInfo = e.detail.userInfo
    //console.log(e.detail.rawData)
    
    wx.getSetting({
      success(res) {
        //没有授权
        if (!res.authSetting['scope.userInfo']) {
          that.setData({
            unlogin: true
          })
        } else {
          that.setData({
            unlogin: false
          })
        }
      }
    })

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        wx.request({
          url: 'https://meile1990.meileholdings.com/miku/index.php/adduser',
          data: {
            openid: res.data,
            nick_name: userInfo.nickName
          },
          method: 'POST',
          success: function (res) {
            // console.log(res.data);
            wx.setStorage({
              key: 'userid',
              data: res.data,
            })
          }
        })
      },
    })
  },



//领取新人券
getNewPerson:function(){
  console.log("点击领取新人券");
  var that = this;
  //1.隐藏新人券
  this.setData({
    getNewPersonQuan:false
  })
  //2.网络请求，获得新人券
  wx.getStorage({
    key: 'userid',
    success: function(res) {
      wx.request({
        url: http_service + '/index.php/newpersonquan',
        data:{
          userId:res.data
        },
        success:function(res){
          //领取成功
          if(res.data.rescode==0){
            wx.showToast({
              title: '领取成功',
              icon: 'success',
              duration: 1000
            })
          }
          if (res.data.rescode == 1){
            wx.showToast({
              title: '不可重复领取',
              icon: 'none',
              duration: 1000
            })
          }
        }
      })
    },
  })
  //3.是否获得成功进行提示
}

  
})  
